$(document).ready(function(){
	var lettersRegex = /([a-zA-Z]+)/;

	// use not to request again if click word is same in previous click word
	var clickedDefinitionWord = ''; 
	var firstWord = '';
	var lastWord = '';

	attachEventOnWord = function() {
		$('.word').off('click');
		$('.word').on('click',function(){
			var spa = $(this);
			var word = $(this).text();
			word = lettersRegex.exec(word);
			word = word[0].toLowerCase();
			if(word == clickedDefinitionWord) {
				return;
			}
			clickedDefinitionWord = word;
			getDefinition(word,spa);
		});
	};

	attachEventOnBackword = function() {
		$('.backword').off('click');
		$('.backword').on('click',function(){
			$(this).parents('li').nextAll().remove();
			getDefinition($(this).html());
		});
	};

	getDefinition = function(clickWord,owner) {
		$('.word').removeClass('wordClick');
		var url = '/index.php/word_service/getDefinition/'+clickWord;
		if(owner != undefined) {
			owner.addClass('wordClick');
		} else {
			url+='/chain';
		}
		

		$.ajax({
			url:url,
			dataType:'json',
			success:function(data) {
				if(data == 'failed') {
					alert('clicked word not in definition');
					return;
				}
				if(data =='won') {
					showWinnerMessage();
					return;
				}
				$('#game_word_chain').append('<li><a class="backword">' + clickWord +
					'</a><span class="divider">/</span></li>');
				$('#game_definition').html(data[0].text);
				$("#game_definition").spanify('span','word');
				$('#game_current_word').html(clickWord);
				attachEventOnWord();
				attachEventOnBackword();
			},
			error:function(e) {
				if(owner != undefined)
					owner.removeClass('wordClick');
			}
		});	
	};

	showWinnerMessage = function() {
		var length = $('#game_word_chain li').length;
		var msg = '<div class="alert alert-success" id="game_winner_message">' +
			'<button type="button" class="close" data-dismiss="alert">&times;' +
			'</button><h4>Winner!</h4><p>You win! Congratulations!</p>' +
			'<a href="https://twitter.com/share" class="twitter-share-button"' +
			'data-url="http://nirdga.me/play/custom/'+firstWord+'/'+lastWord +
			'" data-text="I won the #Nird game '+firstWord+' and '+lastWord +
			' in '+length+' links!" data-via="Nirdgame" data-size="large"> ' + 
			'Tweet</a></div>';
		$('#game_container').html(msg);
		$("#game_container").append('<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\'://platform.twitter.com/widgets.js\';fjs.parentNode.insertBefore(js,fjs);}}(document, \'script\', \'twitter-wjs\');<\/script>');
		
	}

	getLastWord = function() {
		$.ajax({
			url:'/index.php/word_service/getRandomLastWord',
			dataType:'json',
			success:function(data){
				lastWord = data.word;
				$('#game_last_word').html(data.word);
			},
			error:function(e){
					$("#game_last_word").text("computer");
			}
		});	
	};
	
	getRandomWord = function() {
		$.ajax({
			url:'/index.php/word_service/getRandomWord',
			dataType:'json',
			success:function(data) {
				$('#game_first_word').html(data.word);
				firstWord = data.word;
				$('#game_current_word').html(data.word);
				$('#game_word_chain').append('<li><a class="backword">' + data.word +
						'</a><span class="divider">/</span></li>');
				$("#game_definition").html(data.definition);
				$("#game_definition").spanify('span','word');
				attachEventOnWord();
				attachEventOnBackword();
			},
			error:function(e) {
				$("#game_first_word").text("failed");
			}
		});
	};

});